// pages/party/party.js
const util = require('../../utils/timeTool.js');
const utils = require('../../utils/util.js');
const qiniu = require("../../utils/qiniu.min.js");
import Dialog from '../../dist/dialog/dialog';
var app = getApp();

Page({
  /**
   * 页面的初始数据
   */
  data:{
    inputShowed: false,
    inputShowed1: false,
    inputShowed2: false,
    timeShow:false,
    initDate: utils.formatTime4(new Date).substring(0, 11),
    birthdayLunarStr:'',
    birthdayDefault:'',
    peoplename:'',
    phone:'',
    currentLength:'',
    blessingtxt:'',
    isText: false,
    ifNotice: false,
    posterUrl:[],
    relationId:'',
    preservationNum:'',
    birthdayTime:'',
    templateTxt: [],
    isTemplate:false,
    partyBack: false,
    isSelection:false,
    senctionIndex:'',
    dateInfo:'',
    isCf:true,
    posterUrl1:'',
    posterUrl2: '',
  },
  timeShow(e){
    this.setData({
      timeShow:true,
      // value: [1, month, day-1]
    })
  },
  submit: function (e) {
    console.log(e);
    console.log('日期类型：' + e.detail.dateType)
    console.log('是否显示年份：' + e.detail.showYear)
    console.log('日期字符串：' + e.detail.dateStr)
    if (e.detail.dateType=='公历'){
      this.setData({
        birthdayTime: e.detail.dateStr +' '+ '00:00:00',
        birthdayDefault:0
      })
      console.log('公历判断', this.data.birthdayTime, this.data.initDate)
    }else{
      console.log('农历', 11111)
      this.setData({
        birthdayLunarStr: e.detail.dateStr,
        birthdayDefault:1
      })
    }
    if(this.data.birthdayDefault==0){
      this.setData({
        dateInfo: util.itemDateToShow(this.data.birthdayTime, 1, false, false).substring(5, 11)
      })
      console.log(this.data.dateInfo)
    }else{
      this.setData({
        dateInfo: this.data.birthdayLunarStr.substring(9, 13)
      })
      console.log(this.data.dateInfo)
    }
  
  },
  preventTouchMove: function (e) {
  },
  // timeok(){
  //   this.setData({
  //     timeShow: false
  //   })
  // },
  // timeno() {
  //   this.setData({
  //     timeShow: false
  //   })
  // },
  //姓名
  peoplenameInput(e) {
    var that = this
    var peoplename = e.detail.value
    that.setData({
      peoplename: peoplename
    })
  },
  peoplenameblur(e) {
    this.setData({
      inputShowed1: false
    })
    var peoplename = e.detail.value
    // if (peoplename == '') {
    //   wx.showToast({
    //     title: '请输入姓名',
    //     icon: 'none',
    //     duration: 2000
    //   })
    //   return this.data.peoplename;
    // } else {
      this.setData({
        peoplename: peoplename
      })
    // }
  },
  //聚焦
 peoplenameShowed(e) {
    this.setData({
      inputShowed1: true,
    })
  },
  //微信账号
  phoneInput(e) {
    var phone = e.detail.value
    this.setData({
      phone: phone
    })
  },
  phoneblur(e) {
    this.setData({
      inputShowed2: false
    })
    var phone = e.detail.value
    // if (phone == '') {
    //   wx.showToast({
    //     title: '请输入手机号',
    //     icon: 'none',
    //     duration: 2000
    //   })
    //   return this.data.phone;
    // } else {
      this.setData({
        phone: phone
      })
    // }
  },
  //聚焦
  phoneShowed(e) {
    this.setData({
      inputShowed2: true,
    })
  },
  //祝福语
  bindTextAreaBlur(e) {
    this.setData({
      blessingtxt: e.detail.value.replace(/\s+/g, ''),
      isText: false,
      ifNotice: false
    })
  },
  ifNotice() {
    this.setData({
      ifNotice: true,
      isText: true
    })
  },
  //模板
  istemplate(){
    var that = this
    that.setData({
      isTemplate: !that.data.isTemplate,
    })
  },

  //获得默认背景图
  getBgImgList(img) {
    var that = this;
    app.req({
      url: 'wechat/image/list',
      data: {
        "type": 12
      },
      method: 'POST',
      success: function (res) {
        if (res.code == 200) {
          console.log('背景图', res)
          for (var i = 0; i < res.data.length; i++) {
            if (res.data[i].sort == 1) {
              that.setData({
                posterUrl1: res.data[i].url,
              })
            }
            if (res.data[i].sort == 2) {
              that.setData({
                posterUrl2: res.data[i].url,
              })
            }
          }
        } else {
          wx.showToast({
            title: res.msg,
            icon: 'none',
            duration: 2000
          })
        }
      }
    })
  },
  // 生日模板接口
  istemplateData () {
    var that = this
    app.req({
      url: "wechat/dictionary/listBydictType",
      method: "post",
      data: {
        dictType: "BLESSINGS",
      },
      success: function (res) {
        if (200 == res.code) {
          console.log("祝福语", res)
          that.setData({
            templateTxt: res.data
          })
        }
      }
    })
  },

  templateno(){
    this.setData({
      isTemplate: !this.data.isTemplate
    })
  },
  templateok() {
    var arr = []
    for(let i = 0;i<this.data.templateTxt.length;i++){
      if(this.data.templateTxt[i].flag){
        arr.push(this.data.templateTxt[i].flag)
        this.setData({
          blessingtxt: this.data.templateTxt[i].dictName,
          isTemplate: !this.data.isTemplate
        })
      }
    }
    if (arr.length <= 0) {
      this.setData({
        blessingtxt: '',
        isTemplate: !this.data.isTemplate
      })
    }
  },
  radioBtn(e){
    console.log(e.currentTarget.dataset.index)
    var index = e.currentTarget.dataset.index
    if (this.data.senctionIndex != index) {
      var templateArray = this.data.templateTxt
      for (let i = 0; i < templateArray.length; i ++) {
        templateArray[i].flag = false
      }
      this.setData({
        templateTxt: templateArray,
        isSelection:false
      })
    }
    this.data.isSelection = !this.data.isSelection
    this.setData({
      isSelection: this.data.isSelection
    })
    var array = this.data.templateTxt
    array[index].flag = this.data.isSelection
    this.setData({
      templateTxt: array
    })

    this.setData({
      senctionIndex: index
    })
  
  },
  //图片
  posterimg() {
    var that = this;
    if (that.data.posterUrl.length >= 3) {
      wx.showToast({
        title: '最多只能上传3张图片',
        icon: 'none',
        duration: 2000
      })
    } else {
      app.getQiNiuToken(function (res) {
        var countnum = 3 - that.data.posterUrl.length
        var qiniuconfig = res;
        wx.chooseImage({
          count: countnum,
          sizeType: ['original', 'compressed'],
          sourceType: ['album', 'camera'],
          success: function (res) {
            const filepathArray = res.tempFilePaths
            for (let i = 0; i < filepathArray.length; i++) {
              qiniu.upload(filepathArray[i], res => {
                that.setData({
                  posterUrl: that.data.posterUrl.concat([res.imageURL])
                })
                // console.log("网络图片", that.data.posterUrl)
              }, (error) => {
                console.log('error' + error)
              }, {
                  uploadURL: 'https://upload.qiniup.com',
                  region: 'ECN',
                  domain: qiniuconfig.qiniuDomain,
                  uptoken: qiniuconfig.upToken,
                })
            }

          }
        })
      });
    }
  },
  picdelete(e){
    var that= this
    var index = e.currentTarget.dataset.index
    that.remove(that.data.posterUrl, index)
  },
  remove: function (array, val) {
    for (var i = 0; i < array.length; i++) {
      if ( i == val) {
        array.splice(i, 1);
      }
    }
    this.setData({
      posterUrl: array
    })
  },
  //手机号检索
  telphoneRetrieval(){
    var that = this
    app.req({
      url: "wechat/birthManage/search",
      method: "post",
      data: {
        telphone: that.data.phone,
      },
      success: function (res) {
        if (200 == res.code) {
          console.log("手机号", res)
          // that.toggle("list", res.data.list);
          if(res.data){
            console.log('有值')
            that.setData({
              relationId: res.data.id
            })
          }
          that.releaseActivity()
        }
      }
    })
  },
  //发活动
  releaseActivity(){
    var that = this
    // var birthdayTime = that.data.year+'-'+that.data.month+'-'+that.data.day+' '+'00:00:00'
    // console.log('时间', birthdayTime)
    // var time = that.data.month + '月' + that.data.day + '日'
    app.req({
      url: "wechat/birthManage/save",
      method: "post",
      data: {
        actType:12,
        birthday:that.data.birthdayTime,
        birthdayLunarStr: that.data.birthdayLunarStr,
        birthdayDefault: that.data.birthdayDefault,
        birthdayPerson: that.data.peoplename,
        wechatPhone: that.data.phone,
        blessings: that.data.blessingtxt,
        sourceType:1,
        urls: that.data.posterUrl,
        relationId: that.data.relationId,
        posterUrl: that.data.posterUrl2
      },
      success: function (res) {
        if (200 == res.code) {
          console.log(that.data.preservationNum)
          if (that.data.preservationNum ==1){
            wx.navigateTo({
              url: '/pages/partySettingMemo/partySettingMemo'
            });
          } else if (that.data.preservationNum == 2){
            wx.navigateTo({
              url: '/pages/partyTitle/partyTitle?id=' + res.data.id
            });
            that.setData({
              isCf:true
            })
          }
        }
      }
    })
  },
  //预览
  invitation(){
    var that = this
    // var birthdayTime = that.data.year + '-' + that.data.month + '-' + that.data.day
    // that.setData({
    //   birthdayTime: birthdayTime
    // })
    if (that.data.peoplename==''){
      wx.showToast({
        title: '请输入姓名',
        icon: 'none',
        duration: 2000
      })
      return;
    }else if(that.data.phone==''){
      wx.showToast({
        title: '请输入微信绑定手机号',
        icon: 'none',
        duration: 2000
      })
      return;
    } else if (!(/^1[3465789]\d{9}$/.test(that.data.phone))){
      wx.showToast({
        title: '请输入正确的号码格式',
        icon: 'none',
        duration: 2000
      })
      return;
    } else if (that.data.birthdayTime == '' && that.data.birthdayLunarStr == ''){
      wx.showToast({
        title: '请选择生日日期',
        icon: 'none',
        duration: 2000
      })
      return;
    }else if(that.data.isCf) {
      that.setData({
        preservationNum: 2,
        isCf:false,
      })
      that.telphoneRetrieval()
    }
  },
  //保存
  tapActivityDetails(){
    var that = this
    // var birthdayTime = that.data.year + '-' + that.data.month + '-' + that.data.day
    // that.setData({
    //   birthdayTime: birthdayTime
    // })
    if (that.data.peoplename == '') {
      wx.showToast({
        title: '请输入姓名',
        icon: 'none',
        duration: 2000
      })
      return;
    } else if (that.data.phone == '') {
      wx.showToast({
        title: '请输入微信绑定手机号',
        icon: 'none',
        duration: 2000
      })
      return;
    } else if (!(/^1[34578]\d{9}$/.test(that.data.phone))) {
      wx.showToast({
        title: that.data.phone,
        icon: 'none',
        duration: 2000
      })
      return;
    } else if (that.data.birthdayTime == '' && that.data.birthdayLunarStr=='') {
      wx.showToast({
        title: '请选择生日日期',
        icon: 'none',
        duration: 2000
      })
      return;
    }else{
      that.setData({
        preservationNum: 1
      })
      that.telphoneRetrieval()
    }
  },
  onShow: function () {
    var that = this
    if (that.data.partyBack){
      wx.switchTab({
        url: '/pages/index/index'
      })
    }
    that.getBgImgList()
  },

  onLoad:function () {
    this.istemplateData ()
  },
})